package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.GoodsCommentDao;
import com.qububu.emporium.comment.dto.GoodsCommentInfoDto;
import com.qububu.utils.SystemUtil;

@Repository
public class GoodsCommentDaoImpl implements GoodsCommentDao {

	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	/**
	 * @author HQ
	 * @功能 根据商品的id查询查看评价详情
	 * 
	 * */
	public List<GoodsCommentInfoDto> getGoodsCommentsByGoodsId(int goodsId) {
		// TODO Auto-generated method stub
		
		List<GoodsCommentInfoDto> goodsCommentInfoDtoList = null;
		goodsCommentInfoDtoList = new ArrayList<GoodsCommentInfoDto>();
		String sql = "select * from goods_detal_msg where goods_id = ? order by msg_create_dttm desc";
		
		List<Map<String, Object>> elemList = jdbcTemplate.queryForList(sql, goodsId);
		//封装
		GoodsCommentInfoDto goodsCommentInfoDto = null;
		for (Map<String, Object> elem : elemList) {
			goodsCommentInfoDto = new GoodsCommentInfoDto((Long) elem.get("msg_id"), (Long) elem.get("goods_id"), (Long) elem.get("user_id"), (Long) elem.get("parent_id") );	
			goodsCommentInfoDto.setMsgGoodsPoint(String.valueOf(elem.get("msg_goods_point")));
			goodsCommentInfoDto.setMsgGoodsPicUrl(String.valueOf(elem.get("msg_goods_pic_url")));
			goodsCommentInfoDto.setMsgCreateIp(String.valueOf(elem.get("msg_goods_create_ip")));
			goodsCommentInfoDto.setMsgCreateDttm(String.valueOf(elem.get("msg_create_dttm")));
			goodsCommentInfoDto.setMsgContent(String.valueOf( elem.get("msg_content")));
			goodsCommentInfoDto.setMsgAgreeNum(String.valueOf(elem.get("msg_agree_num")));
			
			goodsCommentInfoDtoList.add(goodsCommentInfoDto);
		}
		return goodsCommentInfoDtoList;
	}

	
	/**
	 * @author HQ
	 * @功能 用户添加评价
	 * @param goodsCommentInfoDto 商品评价信息
	 */
	public void doAddGoodsComment(GoodsCommentInfoDto goodsCommentInfoDto) {
		// TODO Auto-generated method stub
		
		String sql = "insert into goods_detal_msg(msg_goods_point, msg_content, msg_create_dttm, msg_create_ip, msg_goods_pic_url, msg_agree_num, goods_id, parent_id, user_id) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
		Object[] showInfoValues = {
				goodsCommentInfoDto.getMsgGoodsPoint(),
				goodsCommentInfoDto.getMsgContent(),
				goodsCommentInfoDto.getMsgCreateDttm(),
				goodsCommentInfoDto.getMsgCreateIp(),
				goodsCommentInfoDto.getMsgGoodsPicUrl(),
//				showInfoDto.getMsgAgreeNum(),
				0,
				//测试时候根据传入的值的类型判断是否需要解密
				SystemUtil.decrypt(goodsCommentInfoDto.getGoodsId()),
				SystemUtil.decrypt(goodsCommentInfoDto.getParentId()),
				SystemUtil.decrypt(goodsCommentInfoDto.getUserId())
				};
		
		jdbcTemplate.update(sql, showInfoValues);
	}
}
